package com.student.controller;


import com.student.entity.User;
import com.student.service.ManageService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authc.AuthenticationException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import java.io.IOException;
import java.util.Map;

/**
 * Created by dongge on 16/9/16.
 */
@Controller
public class UserController {

    private final static String RLIST = "redirect:/users";
    private final static String LIST = "user/list";
    private final static String INPUT = "user/input";

    @Autowired
    private ManageService manageService;

    @RequestMapping("/users")
    public String list(Map<String,Object> map){
        map.put("users",manageService.getAll(User.class));
        return LIST;
    }

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public String input(Map<String,Object> map){
        map.put("user",new User());
        map.put("users",manageService.getAll(User.class));
        return INPUT;
    }

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public String create(User user){
        manageService.save(User.class, user);
        return RLIST;
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public String edit(@PathVariable("id")Integer id, Map<String,Object> map){
        map.put("user",manageService.get(User.class,id));
        map.put("users", manageService.getAll(User.class));
        return INPUT;
    }

    /**
     * 有问题还要改才行??????
     * @param id
     * @param parentId
     * @param map
     */
    @ModelAttribute
    public void getUser(@RequestParam(value = "id",required = false)Integer id,
                        @RequestParam(value = "parent.id",required = false)Integer parentId,
                        Map<String,Object> map){
        if(id !=null){
            User user = (User)manageService.get(User.class,id);
            map.put("user",user);
        }
    }

    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public String update(User user){
        manageService.update(User.class,user);
        return RLIST;
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("id")Integer id){
        manageService.delete(User.class,id);
        return RLIST;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(User user,ServletRequest request,ServletResponse response) {
        String fallbackUrl = "/";
        String successUrl = null;
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
        subject.login(token);
        if(subject.isAuthenticated()){
            SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(request);
            if (savedRequest != null && savedRequest.getMethod().equalsIgnoreCase(AccessControlFilter.GET_METHOD)) {
                successUrl = savedRequest.getRequestUrl();
            }
            if(successUrl == null){
                return "redirect:"+fallbackUrl;
            } else {
                return "redirect:"+successUrl;
            }
        }else {
            /**
             * 实际上不会运行到这一句
             */
            return "redirect:/login.jsp";
        }
    }

//    @ExceptionHandler(AuthenticationException.class)
//    public String exceptionHandle(Exception ex){
//        System.out.println(ex);
//        return "error";
//    }
}
